﻿using LoveEat.API.Read.Applications.Commands.Goods;
using LoveEat.API.Read.DTOs;
using LoveEat.Domain.Goods;
using LoveEat.ErrorCode;
using LoveEat.Infrastructure.Interfaces;
using MediatR;
using Microsoft.AspNetCore.Server.HttpSys;
using NLog.LayoutRenderers.Wrappers;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace LoveEat.API.Read.Applications.CommandHandlers.Goods
{
    public class GoodsBackQueryCommandHandler : IRequestHandler<GoodsBackQueryCommand, APIResult<List<GoodsDTO>>>
    {
        private readonly IBaseRespository<GoodsModel> goodsRep;
        private readonly IBaseRespository<GoodsNameModel> nameRep;

        public GoodsBackQueryCommandHandler(IBaseRespository<GoodsModel> goodsRep,
            IBaseRespository<GoodsNameModel> nameRep)
        {
            this.goodsRep = goodsRep;
            this.nameRep = nameRep;
        }

        public Task<APIResult<List<GoodsDTO>>> Handle(GoodsBackQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<GoodsDTO>> result=new APIResult<List<GoodsDTO>>();
            result.Code=APIResultCode.查询成功;
            result.Message=APIResultCode.查询成功.ToString();

            var res = from a in goodsRep.GetAll()
                      join b in nameRep.GetAll() on a.GoodsNameId equals b.GoodsNameId
                      where !a.IsDelete && a.GoodsId==request.GoodsId
                      select new GoodsDTO
                      {
                          GoodsId = a.GoodsId,
                          GoodsNameId = a.GoodsNameId,
                          Production = a.Production,
                          LifeDay = a.LifeDay,
                          Pronumber = a.Pronumber,
                          ProCommand = a.ProCommand,
                          Path = a.Path,
                          Information = a.Information,
                          CreateName = a.CreateName,
                          CreateTime = a.CreateTime,
                          UpdateName = a.UpdateName,
                          AquacultureId = a.AquacultureId,
                          slaughterId = a.slaughterId,
                          DeepId = a.DeepId,
                          GoodsNameName = b.GoodsNameName,
                      };

            result.Data=res.ToList();

            return Task.FromResult(result);
        }
    }
}
